# Copyright 2004-2010 PyTom <pytom@bishoujo.us>
# See LICENSE.txt for license details.

init python:

    layout.provides('joystick_preferences')
    
    style.js_frame = Style(style.menu_frame, help="frame used for changing a joystick binding")
    style.js_frame_box = Style(style.vbox, help="vbox used for changing a joystick binding")
    style.js_function_prompt = Style(style.prompt, help="the name of the joystick binding to change (window)")
    style.js_function_prompt_text = Style(style.prompt_text, help="the name of the joystick binding to change (text)")
    style.js_action_prompt = Style(style.prompt, help="prompting the user how to change a joystick binding (window)")
    style.js_action_prompt_text = Style(style.prompt_text, help="prompting the user how to change a joystick binding (text)")

    style.js_prefs_frame = Style(style.menu_frame, help="frame containing all joystick bindings")
    style.js_prefs_box = Style(style.vbox, help="box containing all joystick bindings")
    style.js_prefs_button = Style(style.button, help="button containing a joystick binding")
    style.js_prefs_button_text = Style(style.button_text, help="button containing a joystick binding (text)")
    
    style.js_prefs_label = Style(style.label, help="joystick preferences label (window)") 
    style.js_prefs_label_text = Style(style.label_text, help="joystick preferences label (text)") 

    style.js_frame.ypos = .1
    style.js_frame.ypadding = .05
    style.js_frame.xmargin = .1
    style.js_frame_box.box_spacing = 30

    style.js_prefs_frame.xpos = 10
    style.js_prefs_frame.ypos = 10

    style.js_prefs_button.xminimum = 0.5
    style.js_prefs_box.box_first_spacing = 10
    
    config.joystick_keys = [
        (u'Left', 'joy_left'),
        (u'Right', 'joy_right'),
        (u'Up', 'joy_up'),
        (u'Down', 'joy_down'),
        (u'Select/Dismiss', 'joy_dismiss'),
        (u'Rollback', 'joy_rollback'),
        (u'Hold to Skip', 'joy_holdskip'),
        (u'Toggle Skip', 'joy_toggleskip'),
        (u'Hide Text', 'joy_hide'),
        (u'Menu', 'joy_menu'),
        ]
    
    def _joystick_select_binding():

        for label, key in config.joystick_keys:

            def my_clicked(label=label, key=key):
                return (label, key)

            layout.button(_(label) + " - " + _(_preferences.joymap.get(key, u"Non assigné")), "prefs_js", clicked=my_clicked, index=label)
    
    def _joystick_get_binding():
        ui.saybehavior()
        ui.add(renpy.display.joystick.JoyBehavior())

    def _joystick_take_binding(binding, key):

        if not isinstance(binding, basestring):
            if key in _preferences.joymap:
                del _preferences.joymap[key]
        else:
            _preferences.joymap[key] = binding
        
            
    def _joystick_preferences():

            def set_binding(label, key):
                layout.navigation(None)

                ui.window(style='js_frame')
                ui.vbox(style='js_frame_box')
                layout.prompt(_(u"Mappage du joystick") + " - " + _(label), "js_function")
                layout.prompt(u'Move the joystick or press a joystick button to create the mapping. Click the mouse to remove the mapping.', 'js_action')
                ui.close()

                _joystick_get_binding()
                binding = ui.interact(mouse="gamemenu")
                _joystick_take_binding(binding, key)
                
                return True

            ui.window(style='js_prefs_frame')
            ui.vbox(style='js_prefs_box')

            layout.label("Joystick Configuration", "js_prefs")
            
            for label, key in config.joystick_keys:

                def clicked(label=label, key=key):
                    return renpy.invoke_in_new_context(set_binding, label, key)

                layout.button(_(label) + " - " + _(_preferences.joymap.get(key, u"Non assigné")), "js_prefs", clicked=clicked, index=label)
            
            ui.close()

label joystick_preferences_screen:

    while True:
        python:
            layout.navigation("joystick_preferences")
            _joystick_preferences()
            ui.interact(mouse="gamemenu")

            
        
